function testAsyc_01() {
    var i = 0;

    let x = 3;

    setTimeout(() => {
        console.log("The current x value is: " + x);
        x += 3;
        i++;
        console.log("The " + i  + "th x's value is: " + x);
    }, 4000);

    for(let j = 4; j <= 10; j++) {
        x = j;
    }
}

function testAsyc_02() { // the old fashion to implement asychronic.
    // the test function
    function double(value) {
        setTimeout(() => setTimeout(console.log, 3000, value * 2), 
                    1000);
    }

    double(3);

    // test the double function whether has returned
    console.log("The double function has returned.")
}

function testPromise_01() {
    let p1 = new Promise((resolve, reject) => resolve());
    setTimeout(console.log, 0, p1);

    let p2 = new Promise((resolve, reject) => reject());
    setTimeout(console.log, 0, p2);
}

function testPromise_02() {
    function onResolved(id) {
        setTimeout(console.log, 0, id, 'resolved');
    }

    function onRejected(id) {
        setTimeout(console.log, 0, id, 'rejected');
    }

    let p1 = new Promise((resolve, reject) => setTimeout(resolve, 3000));
    let p2 = new Promise((resolve, reject) => setTimeout(reject, 3000));

    p1.then(() => onResolved('p1'),
            () => onRejected('p1'));

    p2.then(() => onResolved('p2'),
            () => onRejected('p2'));
}

testPromise_02();
